/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.customer;

import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.jrcrm.constant.CustomerConstant;
import net.dgg.dggcloud.service.jrcrm.dao.customer.DepartmentCustomerDao;
import net.dgg.dggcloud.service.jrcrm.entity.customer.Customer;
import net.dgg.dggcloud.service.jrcrm.entity.customer.DepartmentCustomer;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.service.cms.RuleConfigService;
import net.dgg.dggcloud.service.service.organization.OrganizationService;
import org.springframework.stereotype.Service;
import net.dgg.dggcloud.service.common.base.service.BaseService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @ClassName 〈DepartmentCustomerService>
 * @Description
 * @Author 朱永波
 * @Date 2017/11/14 10:18
 */
@Service
public class DepartmentCustomerService extends BaseService {

    @Resource
    private DepartmentCustomerDao departmentCustomerDao;
    @Resource
    private RuleConfigService ruleConfigService;
    @Resource
    private OrganizationService organizationService;

    /**
     * 新增事业部客户数据
     *
     * @param map 数据详情
     *            operateUser 操作人
     *            businessId 商机id
     *            dropUserId 掉库人id
     *            customerId 客户id
     *            dropWay 掉库方式
     */
    public void addDepartmentCustomer(Map map) {
        if (null == map.get("operateUser")) {
            throw new BizException("操作人不能为空", 10001);
        }
        if (null == map.get("dropUserId")) {
            throw new BizException("掉库人id不能为空", 10001);
        }
        if (null == map.get("businessId")) {
            throw new BizException("商机id不能为空", 10001);
        }
        if (null == map.get("customerId")) {
            throw new BizException("客户id不能为空", 10001);
        }
        if (null == map.get("dropWay")) {
            throw new BizException("掉库方式不能为空", 10001);
        }
        if (null == map.get("organizationId")) {
            throw new BizException("事业部id不能为空", 10001);
        }

        DepartmentCustomer dc = new DepartmentCustomer();
        dc.setId(keyWorker.nextId());
        dc.setCreateUser((User) map.get("operateUser"));
        dc.setDropBusinessId(Long.valueOf(map.get("businessId").toString()));
        dc.setOrganizationId(Long.valueOf(map.get("organizationId").toString()));
        dc.setDropTime(new Date());
        dc.setDropUserId(Long.valueOf(map.get("dropUserId").toString()));
        dc.setDropWay(map.get("dropWay").toString());
        dc.setCustomerId(Long.valueOf(map.get("customerId").toString()));
        dc.setPushStatus(0);
        if (null != map.get("lockDays")) {//锁定的天数
            Integer days = Integer.valueOf(map.get("lockDays").toString());
            dc.setLockTime(DateUtil.dataAddNumber(new Date(),5,days));
        }
        repository.save(dc);
    }

    /**
     * 根据客户id获取事业部客户数据
     *
     * @param customerId 客户id
     * @return
     */
    public DepartmentCustomer getDepartmentCustomerByCustomerId(Long customerId) {
        if (null == customerId) {
            throw new BizException("客户id不能为空", 10001);
        }
        return repository.findUniqueByProperty(DepartmentCustomer.class, "customerId", customerId);
    }

    /***
     * 查询事业部客户
     * @param map
     * @return
     */
    public List<Map> queryDepartmentCustomerList(Map map){
        return departmentCustomerDao.queryDepartmentCustomerList(map);
    }

    /***
     * 查询事业部客户-count
     * @param map
     * @return
     */
    public Integer queryDepartmentCustomerListCount(Map map){
        return departmentCustomerDao.queryDepartmentCustomerListCount(map);
    }

    /**
     * 事业部客户拨打电话回写是否拨打，以及更新对应事业部客户的隐藏天数
     * @param  customerId：客户id
     * @param  user：操作人
     */
    public void updateDepartmentCusLockTime(Long customerId, User user) throws ParseException {
        if(null != customerId){
            DepartmentCustomer departmentCustomer = repository.findUniqueByProperty(DepartmentCustomer.class, "customerId", customerId);
            if(null != departmentCustomer){
                //查询登录人所在事业部
                Long deptId = organizationService.getOrganizationDepartmentId(user.getOrgId());
                //查询事业部客户拨打隐藏天数
                String days = ruleConfigService.getRuleConfigByMap(deptId, CustomerConstant.GZFL_SYB_3);
                Integer hideDays = StringUtils.isNotEmpty(days) ? Integer.parseInt(days) : 0;
                Date lockTime;
                lockTime = null != departmentCustomer.getLockTime() ? departmentCustomer.getLockTime() : new Date();
                departmentCustomer.setLockTime(DateUtil.dateAddDay(lockTime, hideDays));
                departmentCustomer.setUpdaterUser(user);
                repository.saveOrUpdate(departmentCustomer);
            }
            Customer customer = repository.get(Customer.class, customerId);
            if(null != customer){
                customer.setLastFollowContent("打电话");
                customer.setLastFollowInfo(user);
                customer.setUpdaterUser(user);
                repository.saveOrUpdate(customer);
            }
        }
    }

}
